﻿using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
using System.Windows;

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

using Microsoft.Win32;

using MessageBox = HandyControl.Controls.MessageBox;

namespace MultiWechat;

public partial class MainViewModel : ObservableObject
{
    [ObservableProperty]
    [NotifyCanExecuteChangedFor(nameof(StartCommand))]
    private string _nums = "1";

    [ObservableProperty]
    [NotifyCanExecuteChangedFor(nameof(StartCommand))]
    [NotifyPropertyChangedFor(nameof(TbIsEnabled))]
    [NotifyPropertyChangedFor(nameof(Visibility))]
    private bool _otherIsChecked;

    [ObservableProperty]
    [NotifyCanExecuteChangedFor(nameof(StartCommand))]
    private bool _threeIsChecked;

    [ObservableProperty]
    [NotifyCanExecuteChangedFor(nameof(StartCommand))]
    private bool _twoIsChecked;

    [ObservableProperty]
    [NotifyCanExecuteChangedFor(nameof(StartCommand))]
    private string _wechatpath = "";

    public static Version? Version => Assembly.GetExecutingAssembly().GetName().Version;

    public static string WindowTitle => $"MultiWechat：微信多开神器 v{Version?.Major}.{Version?.Minor} by SHE";

    public bool CanStart => !String.IsNullOrEmpty(Wechatpath) && (TwoIsChecked || ThreeIsChecked || (OtherIsChecked && !String.IsNullOrEmpty(Nums)));

    public bool TbIsEnabled => OtherIsChecked;

    public Visibility Visibility => OtherIsChecked ? Visibility.Visible : Visibility.Hidden;

    // public bool CanStart => !String.IsNullOrEmpty(Wechatpath) && !String.IsNullOrEmpty(Nums) && OneIsChecked && TwoIsChecked && ThreeIsChecked;

    [RelayCommand]
    public void Closing(CancelEventArgs e)
    {
    }

    [RelayCommand]
    public async Task Loaded()
    {
        try
        {
            var registryKey1 = Registry.LocalMachine.OpenSubKey(@$"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\WeChat");
            var registryKey2 = Registry.LocalMachine.OpenSubKey(@$"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Weixin");

            if (registryKey1?.GetValue("InstallLocation") != null)
            {
                Wechatpath = ((string)registryKey1!.GetValue("InstallLocation")! + @$"\WeChat.exe").Replace("\"", "");
            }
            else
            {
                Wechatpath = ((string)registryKey2!.GetValue("InstallLocation")! + @$"\Weixin.exe").Replace("\"", "");
            }

            await Task.Delay(20);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "错误提示", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }

    [RelayCommand]
    public async Task ReadAsync()
    {
        var openFile = new OpenFileDialog { Filter = "微信文件 (.exe)|*.exe" };

        bool? result = openFile.ShowDialog();

        if (result == true)
        {
            Wechatpath = openFile.FileName;
        }

        await Task.Delay(20);
    }

    [RelayCommand(CanExecute = nameof(CanStart))]
    public void Start()
    {
        try
        {
            if (!(Process.GetProcessesByName("WeChat").Length == 0) || !(Process.GetProcessesByName("Weixin").Length == 0))
            {
                MessageBox.Show("发现已有微信程序启动，多开前请先关闭所有微信程序！", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                if (OtherIsChecked == true)
                {
                    if (Convert.ToInt32(Nums) < 11)
                    {
                        for (int i = 0; i < Convert.ToInt32(Nums); i++)
                        {
                            Process.Start(Wechatpath);
                        }
                    }
                    else
                    {
                        MessageBox.Show("多开数量限制在10个以内！", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                }

                if (TwoIsChecked == true)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        Process.Start(Wechatpath);
                    }
                }

                if (ThreeIsChecked == true)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        Process.Start(Wechatpath);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "错误提示", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }
}
